package com.ryder.petmatediarybackend.module.test.controller;

import com.ryder.petmatediarybackend.common.response.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述: Hello 测试接口
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025/10/26
 */
@RestController
@RequestMapping("/test")
@Tag(name = "测试接口", description = "用于测试系统功能的接口")
public class HelloController {

    @GetMapping("/hello")
    @Operation(
        summary = "Hello 测试",
        description = "简单的 Hello World 测试接口，用于验证系统是否正常运行"
    )
    @ApiResponses({
        @ApiResponse(
            responseCode = "200",
            description = "请求成功",
            content = @Content(
                mediaType = "application/json",
                examples = @ExampleObject(
                    value = "{\"success\":true,\"code\":200,\"message\":\"成功\",\"data\":\"Hello, PetMateDiary! 系统运行正常 🎉\"}"
                )
            )
        )
    })
    public Result<String> hello() {
        return Result.ok("Hello, PetMateDiary! 系统运行正常 🎉");
    }

    @GetMapping("/info")
    @Operation(
        summary = "系统信息",
        description = "获取系统基本信息，包括项目名称、版本号、服务器时间、Java版本等"
    )
    @ApiResponses({
        @ApiResponse(
            responseCode = "200",
            description = "查询成功",
            content = @Content(
                mediaType = "application/json",
                examples = @ExampleObject(
                    value = """
                        {
                          "success": true,
                          "code": 200,
                          "message": "成功",
                          "data": {
                            "projectName": "PetMateDiary 伴宠日记",
                            "version": "v1.0.0",
                            "author": "Ryder",
                            "serverTime": "2025-10-26T17:00:00",
                            "javaVersion": "17.0.1",
                            "osName": "Windows 11"
                          }
                        }
                        """
                )
            )
        )
    })
    public Result<Map<String, Object>> info() {
        Map<String, Object> data = new HashMap<>();
        data.put("projectName", "PetMateDiary 伴宠日记");
        data.put("version", "v1.0.0");
        data.put("author", "Ryder");
        data.put("serverTime", LocalDateTime.now());
        data.put("javaVersion", System.getProperty("java.version"));
        data.put("osName", System.getProperty("os.name"));
        return Result.ok(data);
    }

    @GetMapping("/echo/{message}")
    @Operation(
        summary = "回声测试",
        description = "返回传入的消息内容，用于测试路径参数传递"
    )
    @Parameter(
        name = "message",
        description = "要回显的消息内容",
        required = true,
        example = "你好"
    )
    @ApiResponses({
        @ApiResponse(
            responseCode = "200",
            description = "回显成功",
            content = @Content(
                mediaType = "application/json",
                examples = @ExampleObject(
                    value = "{\"success\":true,\"code\":200,\"message\":\"成功\",\"data\":\"Echo: 你好\"}"
                )
            )
        )
    })
    public Result<String> echo(@PathVariable String message) {
        return Result.ok("Echo: " + message);
    }

    @PostMapping("/greet")
    @Operation(
        summary = "问候接口",
        description = "根据传入的名字返回问候语，用于测试POST请求和JSON请求体"
    )
    @io.swagger.v3.oas.annotations.parameters.RequestBody(
        description = "请求参数",
        required = true,
        content = @Content(
            mediaType = "application/json",
            schema = @Schema(implementation = Map.class),
            examples = {
                @ExampleObject(
                    name = "示例1",
                    summary = "普通问候",
                    value = "{\"name\":\"Ryder\"}"
                ),
                @ExampleObject(
                    name = "示例2",
                    summary = "中文名字",
                    value = "{\"name\":\"小明\"}"
                )
            }
        )
    )
    @ApiResponses({
        @ApiResponse(
            responseCode = "200",
            description = "问候成功",
            content = @Content(
                mediaType = "application/json",
                examples = @ExampleObject(
                    value = "{\"success\":true,\"code\":200,\"message\":\"成功\",\"data\":\"Hello, Ryder! Welcome to PetMateDiary! 🐾\"}"
                )
            )
        )
    })
    public Result<String> greet(@RequestBody Map<String, String> request) {
        String name = request.getOrDefault("name", "Guest");
        return Result.ok("Hello, " + name + "! Welcome to PetMateDiary! 🐾");
    }
}
